package com.coocaa.ops.admin.v1.hvi.validator;


import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.audit.provider.BaseAuditTaskProviderImpl;
import com.coocaa.ops.admin.v1.hvi.model.HistoryQueryModel;
import com.coocaa.ops.common.core.base.model.ResourceHistoryInfoModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author bijiahao
 * @date : 2018/6/13.
 * @description
 */
@Component
public class HistoryInfoValidator {

    private static CommonLanguageProperties commonLanguageProperties;


    public static void valid(HistoryQueryModel queryModel) {
        Assert.notNull(queryModel.getId(), commonLanguageProperties.getResourceId()+commonLanguageProperties.getNotNull());
        Assert.hasText(queryModel.getType(), commonLanguageProperties.getResourceType()+commonLanguageProperties.getNotNull());
    }
    @Autowired
    public void setEnterKeyLetterArt(CommonLanguageProperties commonLanguageProperties) {
        HistoryInfoValidator.commonLanguageProperties = commonLanguageProperties;
    }

    /**
     * 是否存在V1版本
     *
     * @param historyInfoList
     * @return
     */
    public static boolean isNeedAddV1(Integer id, List<ResourceHistoryInfoModel> historyInfoList) {
        return CollectionUtils.isNotEmpty(historyInfoList)
                && !isExistV1(historyInfoList)
                ;
    }

    private static boolean isExistV1(List<ResourceHistoryInfoModel> historyInfoList) {
        if (CollectionUtils.isEmpty(historyInfoList)) {
            return false;
        }
        for (ResourceHistoryInfoModel resourceHistoryInfoModel : historyInfoList) {
            if (BaseAuditTaskProviderImpl.V_1.equals(resourceHistoryInfoModel.getVersion())) {
                return true;
            }
        }
        return false;
    }
}
